<layout name="all@index/layout" />
<script type="text/javascript">
    $(function(){
        $('#dg').datagrid({
            title:'用户列表',idField:'username', striped:'true',pagination:'true',rownumbers:true,singleSelect:true,pageSize:30,
            url:'{$ROOT}/admin/user/query',toolbar:'#toolbar',
            columns:[[
            {field:'username',title:'登录名*',width:50,align:'center',editor:{type:'validatebox',options:{validType:'maxLength[45]',required:true}}},
            {field:'teacherno',title:'教师号*',width:50,align:'center',editor:{type:'validatebox',options:{validType:'equalLength[6]',required:true}}},
            {field:'name',title:'姓名*',width:50,align:'center',editor:{type:'validatebox',options:{validType:'maxLength[40]',required:true}}},
            {field:'enteryear',title:'入校年*',width:50,align:'center',editor:{type:'validatebox',options:{validType:'equalLength[4]',required:true}}},
            {field:'sex',title:'性别*',width:40,align:'center', editor:{type:'combobox', options:{url:'{$ROOT}/all/option/sex',valueField:'sex',textField:'name',panelHeight:'auto',required:true,
                onSelect:function(rec) {
                    var tt=$('#dg');
                    tt.datagrid('getSelected').sex=rec.name;
                }}},
                formatter:function(val,rec){
                    return rec.sexname;
                }},
            {field:'role',title:'角色*',width:100,align:'center',editor:{type:'validatebox',options:{validType:'maxLength[20]',required:true}}},
            {field:'school',title:'学院*',width:100,align:'center', editor:{type:'combobox', options:{url:'{$ROOT}/all/option/school',valueField:'school',textField:'name',panelWidth:'120',required:true,
                onSelect:function(rec) {
                    var tt=$('#dg');
                    tt.datagrid('getSelected').schoolname=rec.name;
                }}},
            formatter:function(val,rec){
                return rec.schoolname;
            }},
            {field:'lock',title:'锁定*',width:40,align:'center',editor:{type:'checkbox',options:{on:1,off:0}}, formatter:function(val,rec){if(val==1)return '√';}},
            {field:'modifydate',title:'修改时间',width:130,align:'center'},
            {field:'logintime',title:'登录时间',width:130,align:'center'},
            {field:'remoteip',title:'登录IP',width:90,align:'center'}
            ]],
            //标题行右键菜单
            onHeaderContextMenu: function(e, field){
                e.preventDefault();
                if (!cmenu_obj.cmenu)//没有的话创建一个
                    $('#dg').datagrid('createColumnMenu',cmenu_obj);
                cmenu_obj.cmenu.menu('show', {
                    left:e.pageX,
                    top:e.pageY
                });
            },
            //点击单元格时候的事件
            onClickCell:function(index, field){
                var tt=$('#dg');
                tt.datagrid('startEditing',{field:field,index:index});
                current_datagrid=tt;
            },
            //数据行上右键菜单
            onRowContextMenu:function(e,rowindex,row){
                var tt=$('#dg');
                tt.datagrid('endEditing');
                if(tt.datagrid('editIndex')!=undefined) return;
                e.preventDefault();  //该方法将通知 Web 浏览器不要执行与事件关联的默认动作（如果存在这样的动作）
                tt.datagrid('selectRow',rowindex);
                $('#menu').menu('show',{
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onSelect:function(index,row){
                var tt=$('#dg2');
                tt.datagrid('uncheckAll');
                var allRole=tt.datagrid('getRows');
                var roles=row.role.split('');
                for(var i=0;i< roles.length;i++){
                    for(var j=0;j<allRole.length;j++)
                        if(roles[i]==allRole[j].role) {
                            tt.datagrid('checkRow',j);
                            break;
                        }
                }
                tt.datagrid('getPanel').panel('setTitle','"'+row.name+'"拥有以下角色：');
            }
        });
        //分配部分数据表格
        $('#dg2').datagrid({
            title:'分配角色',idField:'role', striped:'true',rownumbers:true,singleSelect:false,
            url:'{$ROOT}/admin/user/role',toolbar:'#toolbar2',
            columns:[[
                {field:'check',title:'选中',checkbox:true},
                {field:'role',title:'角色代码',width:70,align:'center'},
                {field:'name',title:'角色',width:160,align:'center'}
            ]],
            onSelect:function(index,row){
                if(!$('#dg').datagrid('getSelected')){
                    $.messager.alert('错误','请先选中一个用户','error');
                    $('#dg2').datagrid('unselectRow',index);
                    return false;
                }
            },
            onRowContextMenu:function(e,rowindex,row){
                var tt=$('#dg2');
                tt.datagrid('endEditing');
                if(tt.datagrid('editIndex')!=undefined) return;
                e.preventDefault();
                tt.datagrid('selectRow',rowindex);
                $('#menu2').menu('show',{
                    left: e.pageX,
                    top: e.pageY
                });
            }
        });
        //著作部分 绑定新建按钮事件
        $("#insert,#menu_insert").click(function(){
            var tt=$('#dg');
            tt.datagrid('endEditing');
            if(tt.datagrid('editIndex')!=undefined) return;
            tt.datagrid('insertRow',{
                index: 0,
                row: {
                    role:'B*'
                }
            });
            tt.datagrid('startEditing',{field:'username',index:0});
            current_datagrid=tt;
        });
        //取消更改
        $("#cancel,#menu_cancel").click(function(){
            var tt= $('#dg');
            tt.datagrid('rejectChanges');
            current_datagrid=null;
        });
        //绑定点击保存按钮事件
        $("#save,#menu_save").click(function(){
            var tt=$('#dg');
            tt.datagrid('endEditing');
            if(tt.datagrid('editIndex')!=undefined) return;
            //获取更改的数据行内容
            var rows=tt.datagrid('getRows');
            rows=tt.datagrid('getChanges','updated');
            var count=0;
            var effectRow = {};
            if(rows.length>0){
                count+=rows.length;
                effectRow["updated"]=$.toJSON(rows);
            }
            //获取删除的行
            rows=tt.datagrid('getChanges','deleted');
            if(rows.length>0){
                count+=rows.length;
                effectRow["deleted"]=$.toJSON(rows);
            }
            //获取添加的行
            rows=tt.datagrid('getChanges','inserted');
            if(rows.length>0){
                count+=rows.length;
                effectRow["inserted"]=$.toJSON(rows);
            }
            if(count<=0) {
                $.messager.alert('提示','没有需要保存的数据！','info');
                return;
            }
            $.post('{$ROOT}/admin/user/update',effectRow,function(result){
                if (result.status==1){
                    tt.datagrid('acceptChanges');
                    tt.datagrid('reload');
                    $.messager.show({	// show error message
                        title: '成功',
                        msg: result.info
                    });
                } else {
                    $.messager.alert('错误',result.info,'error');
                }
            },'json');
        });
        //右键菜单删除按钮
        $("#menu_delete,#delete").click(function(){
            tt=$('#dg');
            var row = tt.datagrid('getSelected');
            if (row) {
                tt.datagrid('endEditing');
                var rowIndex = tt.datagrid('getRowIndex', row);
                tt.datagrid('deleteRow', rowIndex);
            }
            else{
                $.messager.alert('错误','请先选中一条记录','error')
            }
        });

        //绑定点击保存按钮事件
        $("#save2,#menu2_save").click(function(){
            var tt=$('#dg2');
            var checkRows=tt.datagrid('getChecked');
            var roles="";
            for(var i=0;i<checkRows.length;i++)
                roles+=checkRows[i].role;
            var userName=$('#dg').datagrid('getSelected').username;
            $.post('{$ROOT}/admin/user/updaterole',{username:userName,role:roles},function(result){
                if (result.status==1){
                    tt.datagrid('acceptChanges');
                    tt.datagrid('reload');
                    $.messager.show({	// show error message
                        title: '成功',
                        msg: result.info
                    });
                    $('#dg').datagrid('reload');
                } else {
                    $.messager.alert('错误',result.info,'error');
                }
            },'json');
        });

        //检索用户
        $("#searchuser").click(function(){
            var tt=$('#dg');
            tt.datagrid('loadData',{total:0,rows:[]});
            tt.datagrid('load', {
                name: $('#name').val(),
                username:$('#username').val(),
                role:$('#role').val(),
                school:$('#school').combobox('getValue')
            });
        });
        //右键修改密码
        $("#menu_changepassword,#changepassword").click(function(){
            var row=$('#dg').datagrid('getSelected');
            if(!row){
                $.messager.alert('错误','请先选择一个用户！','error');
                return;
            }
            $.messager.prompt('修改', '请输入新密码:', function(data){
                if (data){
                    $.post('{$ROOT}/admin/user/changepassword',{teacherno:row.teacherno,password:data},function(result){
                        if (result.status==1){
                            $.messager.show({	// show error message
                                title: '成功',
                                msg: result.info
                            });
                        } else {
                            $.messager.alert('错误',result.info,'error');
                        }
                    },'json');
                }
            });
        });
        $('#school').combobox({
            url:'{$ROOT}/all/option/school?only=0',
            valueField:'school',
            textField:'name'
        });

    });
</script>
<div class="container" style="width:1225px">
    <div id="menu" class="easyui-menu" style="width:100px;">
        <div id='menu_insert' data-options="iconCls:'icon icon-add'">新增用户</div>
        <div id='menu_save' data-options="iconCls:'icon icon-save'">保存</div>
        <div id='menu_changepassword' data-options="iconCls:'icon icon-shield'">修改密码</div>
        <div class="menu-sep"></div>
        <div id='menu_delete' data-options="iconCls:'icon icon-remove'">删除</div>
        <div id='menu_cancel' data-options="iconCls:'icon icon-cancel'">取消</div>
    </div>
    <div id="menu2" class="easyui-menu" style="width:100px;">
        <div id='menu2_save' data-options="iconCls:'icon icon-save'">保存用户角色</div>
    </div>
    <div id="left" style="width:905px;float:left">
        <div id="toolbar">
            <div>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon icon-add',plain:'true'"  id="insert">新增</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon icon-remove',plain:'true'"  id="delete">删除</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon icon-save',plain:'true'" id="save">保存</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon icon-cancel',plain:'true'" id="cancel">取消</a>
                <label for="username">登录名：</label><input id="username" class="easyui-validatebox" size="4" value="%"/>
                <label for="name">姓名：</label><input id="name" class="easyui-validatebox" size="3" value="%"/>
                <label for="role">角色：</label><input id="role" class="easyui-validatebox" size="3" value="%"/>
                <label for="school">学院：</label>
                <input id="school">
                <a href="javascript:void(0)" class="easyui-linkbutton"  data-options="iconCls:'icon icon-search',plain:'true'" id="searchuser">检索</a>
                <a href="javascript:void(0)" class="easyui-linkbutton"  data-options="iconCls:'icon icon-shield',plain:'true'" id="changepassword">修改密码</a>
            </div>
        </div>
        <table id="dg">
        </table>
    </div>
    <div id="right" style="width:310px;float:left;padding-left:5px;">
        <div id="toolbar2">
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon icon-save',plain:'true'" id="save2">保存角色配置</a>
        </div>
        <table id="dg2"></table>
    </div>

    <div class="information" style="clear:both">
        <ol>说明：
            <li>有*标注的为可编辑单元，点击后可以修改内容。</li>
            <li>教师号设置后无法修改</li>
        </ol>
    </div>
</div>